﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ENG.EngStartQueueLib
{
  public class PerformanceMeter
  {
    private PerformanceCounter c = null;
    private bool isMaxOk = true;
    private double okPercentage;
    private double maximum = 1;

    public PerformanceMeter(string categoryName, string counterName, string instance, 
      bool isMaxOk, double okPercentage)
    {
      c = new PerformanceCounter(
        categoryName, counterName, instance);

      this.isMaxOk = isMaxOk;
      this.okPercentage = okPercentage;
    }

    public double GetValue()
    {
      double v;
      double p;
      bool o;
      GetInfo(out v, out p, out o);
      return v;
    }

    public double GetPercentage()
    {
      double v;
      double p;
      bool o;
      GetInfo(out v, out p, out o);
      return p;
    }

    public bool IsOk()
    {
      double v;
      double p;
      bool o;
      GetInfo(out v, out p, out o);
      return o;
    }

    public void GetInfo(out double percentage, out bool isOk)
    {
      double v;
      GetInfo(out v, out percentage, out isOk);
    }

    public void GetInfo(out double value, out double percentage, out bool isOk)
    {
      value = c.NextValue();

      if (maximum < value)
        maximum = value;

      percentage = value / maximum;

      if (isMaxOk)
        if (percentage > okPercentage)
          isOk= true;
        else
          isOk=false;
      else
        if (percentage < okPercentage)
          isOk=true;
        else
          isOk = false;
    }

  }
}
